using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class GoldCoin : MonoBehaviour
{
    public int value = 6;
    private Transform playerTr;
    private Rigidbody2D rigidbody2D;
    public bool canFly = false;
    public bool hasTarget = false;
    public float distance = 4;
    public Vector3 rotateY = new Vector3(0, -90, 0);

    private void Awake()
    {
        value = Random.Range(6, 20);
        playerTr = GameController.Instance().playerCtr.transform;
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        var diff = transform.position - playerTr.position;
        var dis = diff.sqrMagnitude;
        if (dis < distance) hasTarget = true;
        if ((!canFly || dis > distance) && !hasTarget) return;
        transform.LookAt(playerTr);
        transform.Rotate(rotateY);
        rigidbody2D.velocity = transform.right * 7f;
    }
    
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.layer.Equals(9))
        {
            GameController.Instance().archiveCtr.TempLevelData.goldNum += value;
            Destroy(gameObject);
        }
    }
}
